<template>
    <div class="container">

        <div class="body_wrap">

            <el-card shadow="hover" ref="content_card">

                <!-- 基本搜索+功能按钮 -->
                <el-form ref="searchForm1" :inline="true" :model="searchForm" size="medium" label-width="">
                    <el-card class="table_tool_bar_wrap" shadow="never">
                        <div class="base_wrap">
                            <div class="left">
                                <el-form-item label="关键字搜索" prop="keyword">
                                    <el-input placeholder="" v-model="searchForm.keyword" clearable/>
                                </el-form-item>
                                <el-button @click="handleSearch" type="primary" size="medium">搜索</el-button>
                                <el-button @click="handleReset" type="info" plain size="medium">重置</el-button>
                            </div>

                            <div class="right">
                                <el-button @click="handleAddOrUpdateVisible()" type="primary" size="medium" icon="el-icon-plus">新增</el-button>
                                <el-button @click="handleDelete()" type="danger" size="medium" icon="el-icon-delete" :disabled="selectionRows.length == 0">删除</el-button>
                            </div>
                        </div>
                    </el-card>
                </el-form>


                <!-- 表格 -->
                <el-table
                    class="mt10"
                    ref="table"
                    size="medium"
                    height="500"
                    empty-text="暂无数据"
                    element-loading-text="给我一点时间"
                    fit border highlight-current-row
                    :stripe="stripe"
                    v-loading="loading"
                    :data="dataList"
                    @selection-change="handleSelection">

                    <el-table-column type="selection" width="55" prop="selection" v-if="isHideColumn('selection')" align="center"></el-table-column>
                    <el-table-column label="序号" type="index" prop="index" v-if="isHideColumn('index')" width="60" align="center" />
                    <el-table-column label="售后状态" prop="type" align="center" width="90">
                        <template slot-scope="scope">
                            <el-tag type="primary" size="small" v-if="scope.row.type === 1">发</el-tag>
                            <el-tag type="danger" size="small" v-else-if="scope.row.type === 2">收</el-tag>
                            <el-tag type="danger" size="small" v-else>-</el-tag>
                        </template>
                    </el-table-column>
                    <el-table-column label="地点" prop="addressName" v-if="isHideColumn('addressName')" align="left" show-overflow-tooltip/>
                    <el-table-column label="收发货人姓名" prop="name" v-if="isHideColumn('name')" align="left" show-overflow-tooltip/>
                    <el-table-column label="收货人电话" prop="phone" v-if="isHideColumn('phone')" align="left" show-overflow-tooltip/>
                    <el-table-column label="省/直辖市" prop="province" v-if="isHideColumn('province')" align="left" show-overflow-tooltip/>
                    <el-table-column label="市" prop="city" v-if="isHideColumn('city')" align="left" show-overflow-tooltip/>
                    <el-table-column label="区" prop="region" v-if="isHideColumn('region')" align="left" show-overflow-tooltip/>
                    <el-table-column label="详细地址" prop="detailAddress" v-if="isHideColumn('detailAddress')" align="left" show-overflow-tooltip/>
                    <el-table-column label="默认地址" prop="isDefault" align="center">
                        <template slot-scope="scope">
                            <i style="color:#67c23a" class="el-icon-success" v-if="scope.row.isDefault==1"></i>
                            <i class="el-icon-error" v-else></i>
                        </template>
                    </el-table-column>

                    <el-table-column label="操作" prop="btn" v-if="isHideColumn('btn')" width="200px" align="left" fixed="right">
                        <template slot-scope="scope">
                            <el-button @click="handleAddOrUpdateVisible(scope.row.id)" type="text" size="small">编辑</el-button>
                            <el-button @click="handleDelete(scope.row)" type="text" size="small">删除</el-button>
                        </template>
                    </el-table-column>

                </el-table>

                <!-- 分页 -->
                <TablePagination
                    :total.sync="total"
                    :page.sync="searchForm.page"
                    :limit.sync="searchForm.limit"
                    @handleSizeChange="handleSizeChange"
                    @handleCurrentChange="handleCurrentChange"/>
            </el-card>

            <!-- 弹窗, 新增 / 修改 -->
            <AddOrUpdate ref="addOrUpdate" @refreshDataList="getDataList"></AddOrUpdate>


        </div>
    </div>
</template>

<script>
import TableMixin from '@/mixin/TableMixin'
import TableTools from '@/views/components/table/table-tools'
import TablePagination from '@/views/components/table/table-pagination'
import AddOrUpdate from './add-or-update-dialog'
export default {
    mixins: [TableMixin],
    components: {
        TablePagination,
        AddOrUpdate
    },

    data() {
        return {
            dataList: [],
            total: 0,
            loading: false,
            selectionRows: [],
            searchFlag: true,
            searchForm: {
                page: 1,
                limit: 10,
                sortColumns: null,
                sortType: null,
                keyword: null,
                addressName: null,
                sendStatus: null,
                receiveStatus: null,
                name: null,
                phone: null,
                province: null,
                city: null,
                region: null,
                detailAddress: null
            }
        }
    },

    created() {
    },

    mounted() {
        this.$nextTick(() => {
            this.getDataList()
        })
    },

    methods: {

        /**
         * 新增或编辑
         */
        handleAddOrUpdateVisible(id) {
            // 表单窗口
            this.$nextTick(() => {
                this.$refs.addOrUpdate.init(id)
            })
        },

        /**
         * 删除
         */
        async handleDelete(row) {
            let ids = []
            if (row) {
                ids.push(row.id)
            }
            if(this.selectionRows) {
                this.selectionRows.filter((item, index) => {
                    ids.push(item.id)
                })
            }
            if (ids.length <= 0) {
                this.$notify({ title: '提示', message: '请选择需要删除的记录！', type: 'info' });
            } else {
                const confirm = await this.$confirm('确定要删除？', '提示', { type: 'warning' }).catch(() => {})
                if(confirm) {
                    const res = await this.$http({
                        url: '/mall/malldeliveryaddress/delete',
                        method: 'POST',
                        data: ids
                    })
                    if (this.ResultMessage(res)) {
                        this.getDataList()
                    }
                }
            }
        },

        /**
         * 获取表格数据
         */
        async getDataList() {
            this.loading = true
            const res = await this.$http({
                url: '/mall/malldeliveryaddress/page',
                method: 'GET',
                params: this.searchForm
            })
            if (this.ResultMessage(res, false)) {
                this.dataList = res.data.records || []
                this.total = res.data.total
            }
            this.loading = false
        },

        /**
         * 搜索
         */
        handleSearch() {
            this.getDataList()
        },
        /**
         * 重新加载数据表格
         */
        handleReload() {
            this.getDataList()
        },
        /**
         * 重置搜索表单
         */
        handleReset() {
            this.$refs.searchForm1.resetFields()
            this.$refs.searchForm2.resetFields()
        }

    }

}
</script>

<style lang="scss" scoped>

</style>
